#include "TcpServer.hpp"
#include <memory> 

using namespace myself ; 

static Response calculatorHelper(const Request &req)
{
    Response resp(0, 0, req._x, req._y, req._op);
    //Response resp(0, 0);
    switch (req._op)
    {
    case '+':
        resp._result = req._x + req._y;
        break;
    case '-':
        resp._result = req._x - req._y;
        break;
    case '*':
        resp._result = req._x * req._y;
        break;
    case '/':
        if (0 == req._y)
            resp._code = 1;
        else
            resp._result = req._x / req._y;
        break;
    case '%':
        if (0 == req._y)
            resp._code = 2;
        else
            resp._result = req._x % req._y;
        break;
    default:
        resp._code = 3;
        break;
    }
    return resp;
}


void NetCal(Connection* conn, std::string& request)
{
    logMessage(DEBUG, "NetCal been Called, get request : %s", request.c_str());
    Request req ; 
    //反序列化
    //反序列化结果存在req中
    if(!req.Deserialized(request))
    {
        return ; 
    }

    //数据处理  处理结果存入resp
    Response resp = calculatorHelper(req);
    //序列化
    std::string sendstr = resp.Serialize();
    //添加报文
    sendstr = Encode(sendstr);
    //提交服务器conn
    conn->_outbuffer = conn->_outbuffer + sendstr ; 
    //所以边缘触发的设置原理是检测到事件发生后将其事件关心标志位置为0？ ？ ？ 
    conn->_tsvr->EnableReadWrite(conn, true, true);
}

int main()
{
    printf("debug");
    std::unique_ptr<TcpServer> ser (new TcpServer()) ;
    ser->Dispather(NetCal);
     
    return 0 ;
}











